/**
 * 
 */
package com.maywide.jdp.system.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.maywide.jdp.core.exception.AppException;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.dao.Dao;
import com.maywide.jdp.dao.PageQueryTemplate;
import com.maywide.jdp.system.entities.Button;
import com.maywide.jdp.system.vo.ButtonTree;
import com.maywide.jdp.system.vo.ModuleButtonCheckedTree;
import com.maywide.jdp.system.vo.ModuleButtonTree;
import com.maywide.jdp.system.vo.Task;

/**
 * 按钮管理Bean
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Component("buttonBean")
@Transactional(propagation = Propagation.REQUIRED)
public class ButtonBeanImpl implements ButtonBean {
	/**
	 * 基础DAO
	 */
	private Dao dao = null;

	/**
	 * @param dao
	 *            the dao to set
	 */
	@Autowired
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	@Override
	public void insert(Object obj) throws Exception {
		dao.insert(obj);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void remove(Class clazz, Serializable id) throws Exception {
		Object obj = dao.find(clazz, id);
		dao.remove(obj);
	}

	@Override
	public void update(Object obj) throws Exception {
		dao.update(obj);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Object load(Serializable id) throws Exception {
		Button button = dao.find(Button.class, id);
		return button;
	}

	@Override
	public void move(String id, String parentID) throws Exception {
		String jql = "update Button as o set o.module.id = :parentID where o.id = :id";
		Query query = dao.createQuery(jql);
		int row = 0;
		query.setParameter("id", id);
		query.setParameter("parentID", parentID);
		row = query.executeUpdate();
		if (row <= 0)
			throw new AppException("未发生移动操作!");
	}

	@Override
	public void move(List<String> ids, String parentID) throws Exception {
		int row = 0;
		String hql = "update Button as o set o.module.id = :parentID where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("ids", ids);
		conditions.put("parentID", parentID);
		row = dao.executeUpdate(hql, conditions);
		if (row <= 0)
			throw new AppException("未发生移动操作!");
	}

	@Override
	public void lock(String id) throws Exception {
		String jql = "update Button as o set o.active = false where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void lockAll(String[] ids) throws Exception {
		String hql = "update Button as o set o.active = false where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql, conditions);
	}

	@Override
	public void unlock(String id) throws Exception {
		String jql = "update Button as o set o.active = true where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void unlockAll(String[] ids) throws Exception {
		String hql = "update Button as o set o.active = true where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql, conditions);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ModuleButtonTree> treeGrid(Map<String, Object> conditions,
			Map<String, Object> parameters) throws Exception {
		if (conditions == null)
			conditions = new HashMap<String, Object>();
		if (!conditions.containsKey("parentID"))
			conditions.put("noParentID", null);
		if (!conditions.containsKey("active"))
			conditions.put("active", true);

		Object roleIDs = null;
		if (conditions.containsKey("roleIDs")) {
			roleIDs = conditions.get("roleIDs");
			conditions.remove("roleIDs");
		}
		List list = dao.executeNamedQuery(
				"com.maywide.jdp.system.entities.Module.treeGridForButton",
				conditions, parameters, ModuleButtonTree.class);

		if (list == null || list.isEmpty()) {
			String roleID = (String) conditions.get("roleID");
			conditions.remove("roleID");
			conditions.remove("active");
			if (roleIDs != null)
				conditions.put("roleIDs", roleIDs);
			if (conditions.containsKey("parentID"))
				conditions.put("moduleID", conditions.remove("parentID"));
			list = dao.executeNamedQuery(
					"com.maywide.jdp.system.entities.Button.treeGrid",
					conditions, parameters, ModuleButtonCheckedTree.class);

			conditions.put("roleID", roleID);
			if (conditions.containsKey("roleIDs"))
				conditions.remove("roleIDs");
			List<ModuleButtonCheckedTree> checkedList = dao.executeNamedQuery(
					"com.maywide.jdp.system.entities.Button.treeGrid",
					conditions, parameters, ModuleButtonCheckedTree.class);
			if (list != null && !list.isEmpty()) {
				if (checkedList != null && !checkedList.isEmpty()) {
					for (ModuleButtonCheckedTree button : checkedList) {
						if (list.contains(button)) {
							list.remove(button);
							list.add(button);
							button.setChecked(true);
						}
					}
				}
			}
		}
		return list;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page page(int first, int limit, String sort, String dir,
			Map<String, Object> conditions, Map<String, Object> parameters)
			throws Exception {
		Page page = null;

		String queryString = "from Button as o";
		String countQueryString = "select count(o.id) from Button as o";
		if (conditions == null)
			conditions = new HashMap<String, Object>();
		if (!conditions.containsKey("moduleID"))
			conditions.put("noModuleID", null);
		PageQueryTemplate template = new PageQueryTemplate(queryString,
				countQueryString, first, limit, sort, dir);
		page = dao.executePageQuery(template, conditions, null);

		return page;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Task> findTasks(String busnessCode, String operatorID)
			throws Exception {
		List<Task> list = null;
		String name = "com.maywide.jdp.system.entities.Button.getTasks";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("busnessCode", busnessCode);
		parameters.put("operatorID", operatorID);
		list = dao.executeNamedQuery(name, null, parameters, Task.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ButtonTree> findByRoleID(String roleID) throws Exception {
		List<ButtonTree> list = null;
		String jql = "from Button as b where o.";
		Query query = dao.createQuery(jql);
		query.setParameter("roleID", roleID);
		list = query.getResultList();
		return list;
	}
}
